package com.atguigu.tingshu.user.context;

import com.atguigu.tingshu.user.strategy.SaveUserPaidRecordStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SaveUserPaidRecordStrategyContext implements ApplicationContextAware {

    // 定义一个Map存储保存购买信息的策略接口的实现类对象
    private static final Map<String , SaveUserPaidRecordStrategy> STRATEGY_MAP = new HashMap<>() ;

    public static SaveUserPaidRecordStrategy getSaveUserPaidRecordStrategy(String itemType) {
        return STRATEGY_MAP.get(itemType) ;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(SaveUserPaidRecordStrategy.class)
                .values().forEach(saveUserPaidRecordStrategy -> {
                    STRATEGY_MAP.put(saveUserPaidRecordStrategy.getItemType() , saveUserPaidRecordStrategy) ;
                });
    }

}
